package com.bdqn.t330.ch10.controller;


import com.bdqn.t330.ch10.entity.SysUser;
import com.bdqn.t330.ch10.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
public class LoginController {

    @Resource
    UserService userService;

    @RequestMapping("/index")
    public String index() {
        return "/index";
    }

    @RequestMapping("/login")
    public String login(String userCode, String userPassword) {
        SysUser sysUser = new SysUser();
        sysUser.setUserCode(userCode);
        sysUser.setUserPassword(userPassword);
        int count = userService.loginBiz(sysUser);
        if (count > 0) {
            //return "/list";
            return "redirect:/user/userList";
        } else {
            throw new RuntimeException("登录异常");
            //转发
            //return "/index";
            //重定向
            //return "redirect:index";
        }
    }

    //声明一个局部异常
//    @ExceptionHandler(value = {RuntimeException.class})
//    public String handlerException(RuntimeException e, HttpServletRequest request) {
//        request.setAttribute("e", e);
//        return "/error";
//    }
}
